<template>
  <div id='supplier_add' class="Row_content" style="width: 850px;">
    <Form inline ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="130">
      <FormItem label="所属组织：" prop="organization">
        <Select v-model="formValidate.organization" style="width:260px" :label-in-value='true' @on-change='onChange'>
          <Option v-for="item in tagList" :value="item.id" :key="item.id">{{ item.tagName }}</Option>
        </Select>
      </FormItem>
      <FormItem label="所属采购员：" prop="buyer">
        <!-- <Input v-model="formValidate.buyer" placeholder="请输入" class='input-wid' style="width: 260px;"></Input> -->
        <Select v-model="formValidate.buyer" style="width:260px" :label-in-value='true' @on-change='changeBuyer'>
          <Option v-for="item in buyerList" :value="item.id" :key="item.id">{{ item.username }}</Option>
        </Select>
      </FormItem>
      <FormItem label="供应商名称：" prop="supplierName">
        <Input v-model="formValidate.supplierName" placeholder="请输入" class='input-wid' style="width: 260px;" :maxlength='30'></Input>
      </FormItem>
      <FormItem label="供应商缩写：" prop="supplierInitil">
        <Input v-model="formValidate.supplierInitil" placeholder="请输入" class='input-wid' style="width: 260px;" :maxlength='5'></Input>
      </FormItem>
      <FormItem label="法人代表：" prop="legal">
        <Input v-model="formValidate.legal" placeholder="请输入" class='input-wid' style="width: 260px;" :maxlength='10'></Input>
      </FormItem>
      <FormItem label="工商登记号：" prop="businessName">
        <Input v-model="formValidate.businessName" placeholder="请输入" class='input-wid' style="width: 260px;" :maxlength='30'></Input>
      </FormItem>
      <FormItem label="注册资金：" prop="fund">
        <Input v-model="formValidate.fund" placeholder="请输入" class='input-wid' style="width: 260px;" :maxlength='30'></Input>
      </FormItem>
      <FormItem label="联系人：" prop="linkman">
        <Input v-model="formValidate.linkman" placeholder="请输入" class='input-wid' style="width: 260px;" :maxlength='30'></Input>
      </FormItem>
      <FormItem label="联系电话：" prop="phoneNum">
        <Input v-model="formValidate.phoneNum" placeholder="请输入" class='input-wid' style="width: 260px;" :maxlength='30'></Input>
      </FormItem>
      <FormItem label="注册地址：" prop="address">
        <Input v-model="formValidate.address" placeholder="请输入" class='input-wid' style="width: 260px;" :maxlength='30'></Input>
      </FormItem>
      <FormItem label="税务登记号：" prop="taxName">
        <Input v-model="formValidate.taxName" placeholder="请输入" class='input-wid' style="width: 260px;" :maxlength='30'></Input>
      </FormItem>
      <FormItem label="邮箱：" prop="email">
        <Input v-model="formValidate.email" placeholder="请输入" class='input-wid' style="width: 260px;" :maxlength='30'></Input>
      </FormItem>
      <FormItem label="票面税率：" prop="rate">
        <Input v-model="formValidate.rate" placeholder="请输入" class='input-wid' style="width: 260px;" :maxlength='10'></Input>
      </FormItem>
      <FormItem label="最小起订金额：" prop="minPrice">
        <Input v-model="formValidate.minPrice" placeholder="请输入" class='input-wid' style="width: 260px;" :maxlength='10'></Input>
      </FormItem>
      <FormItem label="备注：" prop="remark">
        <Input v-model="formValidate.remark" placeholder="请输入" class='input-wid' style="width: 260px;" :maxlength='30'></Input>
      </FormItem>
      <FormItem label="是否显示：" prop='isShow' style="display: block;">
        <RadioGroup v-model="formValidate.isShow">
          <Radio label="1" >显示</Radio>
          <Radio label="0" style="margin-left: 25px">隐藏</Radio>
        </RadioGroup>
      </FormItem>
      <FormItem>
        <Button type="primary" class="btn" style="width: 80px;" @click="handleSubmit">确定</Button>
        <Button class="btn" style="margin-left: 24px;width: 80px;" @click="handleReset">取消</Button>
      </FormItem>
    </Form>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { routeEqual } from '@/libs/util'
import { supplierAdd, getOrgTag } from '@/api/purchase/index'
import { getEmployeeQuery } from '@/api/system/employee.js'
export default {
  name: 'supplier_add',
  computed: {
    ...mapState({
      merchantId: state => state.user.merchantId,
      userId: state => state.user.userId,
      userName: state => state.user.userName,
      orgGuid: state => state.user.orgId
    })
  },
  data () {
    return {
      buyerName: '',
      buyerList: [],
      submitLock: false,
      orgTagName: '',
      tagList: [],
      formValidate: {
        organization: '',
        supplierName: '',
        supplierInitil: '', // 供应商缩写
        buyer: '',
        legal: '', // 法人代表
        businessName: '', // 工商登记号
        fund: '', // 注册资金
        linkman: '', // 联系人
        phoneNum: '', // 联系电话
        address: '', // 地址
        taxName: '', // 税务登记号
        email: '',
        rate: '', // 票面税率
        minPrice: '', // 最小起订金额
        remark: '',
        isShow: '1'
      },
      ruleValidate: {
        organization: { required: true, message: '请选择所属组织', trigger: 'change' },
        supplierName: { required: true, message: '请填写供应商名称', trigger: 'change' },
        supplierInitil: { required: true, message: '请填写供应商缩写', trigger: 'change' },
        buyer: { required: true, message: '请填写所属采购员', trigger: 'change' },
        // phoneNum: {
        //   validator: (rule, value, callback) => {
        //     if (/^1\d{10}$/.test(value)) {
        //       callback()
        //     } else if (!value) {
        //       callback()
        //     } else {
        //       callback(new Error('请输入正确的手机号'))
        //     }
        //   },
        //   trigger: 'blur'
        // },
        email: {
          validator: (rule, value, callback) => {
            let myReg = /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,5}$/
            if (myReg.test(value)) {
              callback()
            } else if (!value) {
              callback()
            } else {
              callback(new Error('请输入格式正确的邮箱'))
            }
          },
          trigger: 'blur'
        }
      }
    }
  },
  mounted () {
    this.getOrgTags()
    this.getBuyerList()
  },
  methods: {
    changeBuyer (val) {
      this.formValidate.buyer = val.value
      this.buyerName = val.label
    },
    onChange (val) {
      this.formValidate.organization = val.value
      this.orgTagName = val.label
    },
    // 获取采购人列表
    getBuyerList () {
      getEmployeeQuery({
        merchantId: this.$store.state.user.merchantId,
        pageIndex: 1,
        pageSize: 99999,
        status: 1, // 1正常 0 停用
        orgId: this.orgGuid
      }).then(res => {
        const result = res.data || {}
        if (result.status === 200) {
          this.buyerList = result.data.records || []
        } else {
          this.$Message.error(`${result.error}`)
        }
      })
    },
    // 获取组织标签
    getOrgTags () {
      getOrgTag({
        merchantId: this.merchantId,
        id: this.orgGuid
      }).then(res => {
        const result = res.data || {}
        if (result.status === 200) {
          this.tagList = result.data || []
        } else {
          this.$Message.error(result.error || result.message)
        }
      })
    },
    handleSubmit () {
      this.$refs.formValidate.validate(valid => {
        if (valid) {
          if (this.submitLock) {
            return
          }
          this.submitLock = true
          let data = {
            merchantId: this.$store.state.user.merchantId,
            orgTagId: this.formValidate.organization,
            orgTagName: this.orgTagName,
            suppName: this.formValidate.supplierName,
            suppNameShorten: this.formValidate.supplierInitil, // 供应商缩写
            legalRepresenter: this.formValidate.legal, // 法人代表
            busiRegisNum: this.formValidate.businessName, // 工商登记号
            regisCapital: this.formValidate.fund, // 注册资金
            contacter: this.formValidate.linkman,
            contactPhone: this.formValidate.phoneNum,
            regisAddr: this.formValidate.address,
            taxRegisNum: this.formValidate.taxName, // 税务登记号
            foxmail: this.formValidate.email, // 邮箱
            nominalTaxrate: this.formValidate.rate, // 票面税率
            miniOrderMoney: this.formValidate.minPrice, // 最小起订金额
            remarks: this.formValidate.remark,
            displayFlag: this.formValidate.isShow,
            createrId: this.userId,
            createrName: this.userName,
            supplierTypeCode: '1',
            buyer: this.buyerName + ';' + this.formValidate.buyer
          }
          supplierAdd(data).then(res => {
            this.submitLock = false
            if (res.data.status === 200) {
              this.$Message.success('新增成功！')
              this.handleReset()
            } else {
              this.$Message.error(res.data.error || res.data.message)
            }
          })
        }
      })
    },
    handleReset () {
      this.$store.commit('closeTag', this.$route)
      this.$store.commit('setTagNavList', this.$store.state.app.tagNavList.filter(item => !routeEqual(this.$route, item)))
      this.$nextTick(() => {
        this.$router.push({
          name: 'supplier_list'
        })
      })
    }
  }
}
</script>
